package com.terios.util;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.entity.BufferedHttpEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;

import com.terios.AuthorizeUser;
import com.terios.http.WeiboResponse;
import com.terios.sqlite.AuthorizeDAO;
import com.weibo.net.WeiboException;
import com.weibo.net.WeiboParameters;

/**
 * SaveLoginParam.java 2012-9-27 下午12:17:16
 * 
 * @author Wang Tengrui
 * 
 *         保存授权信息
 * 
 */
public class SaveLoginParam {

	/**
	 * 保存当前/上次登录用户的信息
	 */
	public static void saveNowUser(Context context, Bundle values) {
		SharedPreferences sharedPreferences = context.getSharedPreferences(
				"NowUser", Activity.MODE_PRIVATE);
		sharedPreferences.edit().putString("uid", values.getString("uid"))
				.putString("access_token", values.getString("access_token"))
				.putString("expires_in", values.getString("expires_in"))
				.putString("expires_date", values.getString("expires_date"))
				.commit();// 提交
		Log.i("SaveLoginParam", "saveNowUser");
	}

	/**
	 * 获取当前/上次登录用户的信息
	 */
	public static WeiboParameters getNowUser(Context context) {
		SharedPreferences sharedPreferences = context.getSharedPreferences(
				"NowUser", Activity.MODE_PRIVATE);
		WeiboParameters nowUserParameters = new WeiboParameters();
		nowUserParameters.add("uid", sharedPreferences.getString("uid", null));
		nowUserParameters.add("access_token",
				sharedPreferences.getString("access_token", null));
		nowUserParameters.add("expires_in",
				sharedPreferences.getString("expires_in", null));
		nowUserParameters.add("expires_date",
				sharedPreferences.getString("expires_date", null));
		Log.i("SaveLoginParam", "getNowUser");
		return nowUserParameters;
	}

	/**
	 * 保存用户信息到数据库中
	 */
	public static void saveAuthorizeUser(Context context, Bundle values) {
		AuthorizeUser nowUser = null;
		WeiboResponse mWeiboResponse = WeiboResponse.getInstance(context);
		WeiboParameters mBundle = new WeiboParameters();
		mBundle.add("uid", values.getString("uid"));
		try {
			JSONObject jsonObject = new JSONObject(
					mWeiboResponse.getUserInfo(mBundle));
			String avatar_large = jsonObject.getString("avatar_large");// 获取用户头像url(大图)
			String screen_name = jsonObject.getString("screen_name");
			Bitmap userIcon = downloadBitmap(avatar_large);

			nowUser = new AuthorizeUser(values.getString("uid"), screen_name,
					values.getString("access_token"),
					values.getString("expires_in"), Bitmap2Bytes(userIcon),
					values.getString("expires_date"));

			AuthorizeDAO authorizeDAO = new AuthorizeDAO(context);// 存入数据库
			if (authorizeDAO.findUid(values.getString("uid"))) {
				authorizeDAO.update(nowUser);
			} else {
				authorizeDAO.add(nowUser);
			}
			Log.i("SaveLoginParam", "saveAuthorizeUser");

		} catch (JSONException e) {
			e.printStackTrace();
		} catch (WeiboException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 下载图片
	 */
	public static Bitmap downloadBitmap(String url) {

		HttpGet httpRequest = new HttpGet(url);
		HttpClient httpclient = new DefaultHttpClient();
		HttpResponse response;
		try {
			response = (HttpResponse) httpclient.execute(httpRequest);
			HttpEntity entity = response.getEntity();
			BufferedHttpEntity bufferedHttpEntity;
			bufferedHttpEntity = new BufferedHttpEntity(entity);
			InputStream is = bufferedHttpEntity.getContent();
			Bitmap bitmap = BitmapFactory.decodeStream(is);
			Log.i("SaveLoginParam", "downloadBitmap");
			return bitmap;
		} catch (ClientProtocolException e1) {
			e1.printStackTrace();
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		return null;
	}

	/**
	 * Bitmap转换成Bytes[]
	 */
	public static byte[] Bitmap2Bytes(Bitmap bm) {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		bm.compress(Bitmap.CompressFormat.PNG, 100, baos);
		Log.i("SaveLoginParam", "Bitmap2Bytes");
		return baos.toByteArray();
	}

	/**
	 * Bytes[]转换成Bitmap
	 */
	public static Bitmap Bytes2Bitmap(byte[] b) {
		if (b.length != 0) {
			Log.i("SaveLoginParam", "Bytes2Bitmap");
			return BitmapFactory.decodeByteArray(b, 0, b.length);
		} else {
			return null;
		}
	}
}
